package com.controller.common;

import com.entity.common.ApiResult;
import com.entity.common.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.common.UserService;
import com.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/2.
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getPageList")
    public Map<String,Object> getPageList(@RequestParam(value = "pager.pageNo",defaultValue ="1" )  Integer pageNum, @RequestParam(value = "pager.pageSize",defaultValue ="10" ) Integer pageSize,User record){
        Map<String, Object> map=new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<User> pageInfo = new PageInfo<>(userService.getPageList(record));
        map.put("rows",pageInfo.getList());
        map.put("pager.totalRows", pageInfo.getTotal());
        map.put("pager.pageNo",pageNum);
        return  map;
    }

    @RequestMapping("/add")
    public ApiResult add(User user){
        Md5Util.setHex(user);
        return userService.addUser(user);
    }

    @RequestMapping("/deleteUser")
    public ApiResult deleteUser(int id){
        return userService.deleteUser(id);
    }



    @RequestMapping("/getUserById")
    public ApiResult getUserById(Integer id){
        return userService.getUserById(id);
    }

    @RequestMapping("/editUser")
    public ApiResult editUser(User user){
        return userService.editUser(user);
    }

    @RequestMapping("/editUserByUserName")
    public ApiResult editUserByUserName(User user){
        Md5Util.setHex(user);
        return userService.editUserByUserName(user);
    }

}
